
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Shape;
import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import java.io.*;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.WindowConstants;
public class TelnetSample extends JDialog{
	private InputStream in;
	private PrintStream out;
	private char prompt;
	private String ip;
	private String mem_usage;
	int deltax;
	int deltay;
	static ClassicPcapExample cpe=new ClassicPcapExample();
	public void runexec() throws Exception{
		InetAddress addr;
		addr = InetAddress.getLocalHost();
		//System.out.println(addr.getHostAddress().toString());
		ip=addr.getHostAddress().toString();
		OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		 long free_mem=operatingSystemMXBean.getFreePhysicalMemorySize();
		 long total_mem=operatingSystemMXBean.getTotalPhysicalMemorySize();
		 //System.out.println(free_mem+"\n"+total_mem+"\n"+free_mem*1.0/total_mem);
		 
		/* Process p=Runtime.getRuntime().exec("wmic OS Get TotalVisibleMemorySize");
		 InputStream fis=p.getInputStream();
		 InputStreamReader isr=new InputStreamReader(fis);
		 BufferedReader br=new BufferedReader(isr);
		 String line=null;
		 int i=1;
		 while((line=br.readLine())!=null)
         {      
             if(i==3){
            	 total_mem=Long.parseLong(line.trim());
            	 break;
             }
             i++;
         }
		 p=Runtime.getRuntime().exec("wmic OS get FreePhysicalMemory");
		 fis=p.getInputStream();
		 isr=new InputStreamReader(fis);
		 br=new BufferedReader(isr);
		 line=null;
		 i=1;
		 while((line=br.readLine())!=null)
         {      
             if(i==3){
            	 free_mem=Long.parseLong(line.trim());
            	 
            	 break;
             }
             i++;
         }*/
		// System.out.println(free_mem+"\n"+total_mem+"\n"+free_mem*1.0/total_mem);
		 double f=(float) (free_mem*1.0/total_mem)*100;
		 f=100-f;
		 f=Math.round(f*100)/100;
		 mem_usage=Double.toString(f);
	}
	public TelnetSample() throws Exception{
		Point p=new Point();
		p.setLocation(1366,50);
		this.setAlwaysOnTop(true);
		this.setLocation(p);
		this.addMouseListener(new MouseListener(){
			public void mousePressed(MouseEvent e){
				JDialog jf = (JDialog)e.getSource();
				deltax=e.getX();
				deltay=e.getY();
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				JDialog jf = (JDialog)e.getSource();
				com.sun.awt.AWTUtilities.setWindowOpacity(jf, 0.85f);
				
			}
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				JDialog jf = (JDialog)e.getSource();
				com.sun.awt.AWTUtilities.setWindowOpacity(jf, 0.65f);
				
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				JDialog jf = (JDialog)e.getSource();
				if(e.getButton()==3){
					
					JPopupMenu popup=new JPopupMenu();
					JMenuItem quit;
					quit = new JMenuItem("quit"); 
					quit.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							System.exit(0);
						}
						
					});
					popup.add(quit);
					popup.show(jf,e.getX(),e.getY());
				}
			}
		});
		this.addMouseMotionListener(new MouseMotionListener(){
			
		    public void mouseDragged(MouseEvent e) {
		    	JDialog jf = (JDialog)e.getSource();
		        
		        jf.setLocation(jf.getX()+e.getX()-deltax,jf.getY()+e.getY()-deltay);
		       }
		       public void mouseMoved(MouseEvent e) {}
		      });
		setUndecorated(true);
		setSize(77,90);
		//setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.65f);
		setVisible(true);
		JLabel iplabel=new JLabel();
		FlowLayout fl=new FlowLayout();
		fl.setHgap(1000);
		this.setLayout(fl);
		JLabel memlabel=new JLabel();
		JLabel uptraffic=new JLabel();
		JLabel downtraffic=new JLabel();
		this.add(iplabel);
		this.add(memlabel);
		this.add(uptraffic);
		this.add(downtraffic);
		this.validate();
		while(true){
			runexec();
			memlabel.setText("Mem:"+mem_usage+"%");
			iplabel.setText(ip);
			downtraffic.setText("down "+Integer.toString(cpe.downdataforshow)+"KB");
			uptraffic.setText("up "+Integer.toString(cpe.updataforshow)+"KB");
			this.invalidate();
			this.validate();
			Thread.sleep(1000);
		}
	}
	  
	public static void main(String[] args) throws Exception {
		cpe.start();
		new TelnetSample();
		/*Pattern ptime = Pattern.compile("cat");
	    Matcher m = ptime.matcher("acat");
	    m.find();
	    System.out.println(m.group());*/
		
		
		
		/*Connection conn=new Connection("192.168.206.128");
		conn.connect();
		conn.authenticateWithPassword("root", "daenerys");
		
		SCPClient scpClient = conn.createSCPClient();
		scpClient.get("/var/log/nagios3/archives/nagios-06-26-2013-00.log", "");
		FileInputStream fis = new FileInputStream("nagios-06-26-2013-00.log");
		BufferedReader br=new BufferedReader(new InputStreamReader(fis)); 
		String temp;
		while((temp=br.readLine())!=null){
			Pattern ptime = Pattern.compile("\\[[0-9]{10}\\]");
		    Matcher m = ptime.matcher(temp);
		    m.find();
		    Pattern pstate = Pattern.compile("SERVICE NOTIFICATION: ");
		    m=pstate.matcher(temp);
		    if(m.find()){
		    	String datastr=temp.substring(m.end(),temp.length()-1);
		    	String[] data=datastr.split(";");
		    	for(int i=0;i<=data.length-1;i++){
		    		System.out.println(data[i]);
		    	}
		    }
		    
		}/*/

		/*try {
			TelnetSample telnet = new TelnetSample("192.168.206.128", "username","password");
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
	}
}